package com.yvon.service.demo.spring.factory.support;


import com.yvon.service.demo.spring.BeanException;
import com.yvon.service.demo.spring.factory.BeanFactory;
import com.yvon.service.demo.spring.factory.config.BeanDefinition;

/**
 * 抽象bean工厂类
 *
 * 继承了DefaultSingletonBeanRegistry所以也就具备了使用单例注册类方法
 * 实现BeanFactory，实现获取Bean的接口，先从单例注册表中获取，获取不到先获取BeanDefinition，在根据BeanDefinition创建bean
 * 获取BeanDefinition和根据BeanDefinition创建Bean定义为抽象方法，由子类自己实现和扩展
 *
 * @author Yvon
 * @since 2022-11-24
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) {
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeanException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeanException;
}
